package com.cn.wanxi.servlet.pageDataFindAll;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.service.IProductsService;
import com.cn.wanxi.service.impl.ProductsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/products")
public class ProductsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");

        String page=req.getParameter("page");
        String id=req.getParameter("typeId");


        ProductModel productModel=new ProductModel();
        productModel.setId(id==null || "".equals(id) ? 0 : Integer.parseInt(id));
        productModel.setPage(page==null || "".equals(page) ? 1 :Integer.parseInt(page));


        // 产品
        IProductsService iProductsService =new ProductsServiceImpl();
        List<ProductModel> productsFind=iProductsService.productsFinds(productModel);
        List<ProductModel> productsTypeFind=iProductsService.productsTypeFind();
        List<ProductModel> idFindProduct=iProductsService.idFindProduct(productModel);
        int number=iProductsService.getPageCount(productModel);


        JSONObject jsonObject=new JSONObject();
        jsonObject.put("productsFind",productsFind);//分页查询
        jsonObject.put("idFindProduct",idFindProduct);
        jsonObject.put("productsTypeFind",productsTypeFind);
        jsonObject.put("number",number);//获得总条数
        jsonObject.put("pa",page);//获得当前页数
        resp.getWriter().println(jsonObject);
    }
}
